package com.gameserver.controllers.battleship.impl;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gameserver.controllers.BaseController;
import com.gameserver.controllers.battleship.GameController;
import com.gameserver.models.battleship.BattleShip;
import com.gameserver.models.dto.DataCollectionDto;
import com.gameserver.models.dto.GameDataDto;


@Controller
public class GameControllerImpl extends BaseController implements GameController{

	@Override
	@RequestMapping(value="/battleship/game", method = RequestMethod.POST)
	public GameDataDto createGame(@RequestParam("player1Id") String player1Id, @RequestParam("player2id") String player2Id) {
		//validate player Ids
		BattleShip bs = new BattleShip(player1Id, player2Id);
		//save battleship
		return bs.read();	
	}

	@Override
	@RequestMapping(value="/battleship/games", method = RequestMethod.GET)
	public DataCollectionDto getGames(@RequestParam("playerId") String playerId) {
		//query the database for games depending on if the player id is filter or not
		DataCollectionDto collection = new DataCollectionDto();
		if(playerId != null){
			//query based on playerId
		}else{
			//query based on nothing or maybe a filter later on
		}	
		//right now its hard coded.
		BattleShip bs1 = new BattleShip("eric.hobensack@gmail.com", "geoman19@gmail.com");
		collection.getItems().add(bs1.read());
		BattleShip bs2 = new BattleShip("eric.hobensack@gmail.com", "mattpark@gmail.com");
		collection.getItems().add(bs2.read());		
		BattleShip bs3 = new BattleShip("geoman19@gmail.com", "mattpark@gmail.com");
		collection.getItems().add(bs3.read());	
		return collection;
	}

	@Override
	@RequestMapping(value="/battleship/game/{id}", method = RequestMethod.GET)
	public GameDataDto getGame(@PathVariable String id) {
		//would validate the gameId
		//if found then it would return the game
		//sample game
		BattleShip bs1 = new BattleShip("bobby.jay@gmail", "jeff.bagwell@gmail.com");
		return bs1.read();
	}

	@Override
	@RequestMapping(value="/battleship/game/{id}", method = RequestMethod.PUT)
	public GameDataDto updateGame(@PathVariable String id, @RequestBody GameDataDto data) {
		//would find the game by id
		//then update the game based on the info in the request
		//in the meantime, heres a sample
		BattleShip bs1 = new BattleShip("greg.jones@gmail.com", "robert@gmail.com");
		return bs1.read();
	}
	
}
